<%-- 
    Document   : userregistration
    Created on : Dec 28, 2012, 3:21:51 PM
    Author     : ngoc.phan
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%
    String fUser = request.getParameter("txfUser");
    String fPass = request.getParameter("txfPass");
    String fEmail = request.getParameter("txfEmail");
    boolean isSubmit = request.getParameter("update") != null;
    boolean isValid = notEmpty(fUser) && notEmpty(fPass) && notEmpty(fEmail);
    Connection connection = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "english";
    String driverName = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "123456";
    if (isValid) {
        try {
            Class.forName(driverName).newInstance();
            connection = DriverManager.getConnection(url + dbName, userName, password);
            PreparedStatement pstmt = connection.prepareStatement("insert into user(userName,password,email) values(?,?,?)");
            pstmt.setString(1, fUser);
            pstmt.setString(2, fPass);
            pstmt.setString(3, fEmail);
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
%>
<%!
    public static boolean notEmpty(String str) {
        return (str != null && str.length() > 0);
    }
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <style>
            .alert-on{
                color: red;
            }
            .alert-off{
                color: red;
                display: none;
            }
        </style>
    </head>
    <body>
        <h1>User Registration</h1>
        <table>
            <form method="post">
                <tr>
                    <td>User:</td>
                    <td><input type="text" name="txfUser" value="<%=fUser == null ? "" : fUser%>"/>
                        <span class="<%=!notEmpty(fUser) && isSubmit ? "alert-on" : "alert-off"%>">Field Require</span></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type="password" name="txfPass" value="<%=fPass == null ? "" : fPass%>"/>
                        <span class="<%=!notEmpty(fPass) && isSubmit ? "alert-on" : "alert-off"%>">Field Require</span>
                    </td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td><input type="text" name="txfEmail" value="<%=fEmail == null ? "" : fEmail%>"/>
                        <span class="<%=!notEmpty(fEmail) && isSubmit ? "alert-on" : "alert-off"%>">Field Require</span></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Update" name="update"/></td>
                </tr>
            </form>
        </table>
    </body>
</html>
